const { getOne, getAll, update, add } = require('../utils/dbTools')
const tableName = 'business'

/**
 *  查询企业列表 
 *  @param {object} map 查询条件
 */
const getBusinessList = async (map) => {
    const result = await getAll(tableName, map);
    return result;
}

/**
 * 根据条件查询企业信息
 * @param {object} map 查询条件
*/
const getBusinessInfo = async (map) => {
    if (!map) { return false }
    const result = await getOne(tableName, map);
    return result;
}

/**
 *  完善企业信息
 *  @param {object} options 企业信息对象
 */
const updateBusinessInfo = async (options) => {
    const { id } = options
    if (!id) return false
    delete options.id
    const result = await update(id, { ...options }, tableName)
    return result
}

/**
 * 新增一条企业信息
 * @param {number} member_id 管理人员id 
 * @param {string} title 企业名称
 * @param {string} phone 企业电话
 * @param {string} province 省份
 * @param {string} city 城市
 * @param {string} county 取消
*/
const addBusinessInfo = async (member_id, title, phone, province, city, county) => {
    if (!member_id || !title || !phone || !province || !city || !county) {
        return false
    }
    // 随机生成企业编号
    const bus_code = `BUSID${Math.random().toString(16).toUpperCase().slice(2)}`
    const result = await add({ member_id, bus_code, title, phone, province, city, county }, tableName)
    if (!result) return false
    return result?.insertId
}


module.exports = {
    getBusinessList,
    getBusinessInfo,
    updateBusinessInfo,
    addBusinessInfo
}